import os
import csv
import json
idlist=[]
with open('E:\\data_pi\\top100list.tsv','w', encoding='utf-8',newline='\n') as fout:
    writer = csv.writer(fout, delimiter='\t')
    for file in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        if not file.endswith('.json'):
            continue
        ht=file.split('_')[1]
        with open('D:\\Work\\Tool\\tiktok\\TikToks\\' + file, 'r', encoding='utf-8', newline='\n') as filename_input:
            lc = 0
            for line in filename_input:
                z = json.loads(line)
                if lc >= 100:
                    break
                k = ''
                if 'id' in z.keys():
                    k = z['id']
                elif 'itemInfos' in z.keys():
                    k = z['itemInfos']['id']
                lc+=1
                if k not in idlist:
                    writer.writerow([ht,k])
                    idlist.append(k)